<?php
// WARNING: this will work only in PHP5
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
require_once(DOKU_INC.'phpincludes/ACC_Form_Base.php');

/**
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author     Stephane Chamberland <dokuwiki@meteokid.ca>
 * @date       2009-01-22
 */
class ACC_Form_Contact extends ACC_Form_Base {

    var $mailTo = 'web@accmontreal.ca';
    var $mailToAllowed = array(); //should be overridden by derived class; set to true for allowed all (danger of spam)
    var $mailToField = 'to_email';
    var $mailFromField = 'from_email';
    var $mailFromNameField = 'from_name';
    var $mailSubjectPrefix = '[ACC Contact] ';

    var $mailSubjectFmtName = 'mailSubjectFmt';
    var $mailBodyFmtName = 'mailBodyFmt';

    var $metaFileSuffix = 'contact'; //to be overridden by derived class

    var $langArray = array(
        'fr' => array(
            'mailSent'       => 'Votre message a &eacute;t&eacute; envoy&eacute;',
            'mailSentError'  => "Erreur d'envoie, SVP contacter l'administrateur a ",
            'formDescFmt' => '
name accmtl_contact
action mail web@accmontreal.ca
Thanks "Votre message a &eacute;t&eacute; envoy&eacute;. Merci!"

Fieldset "ACC Montreal Contact Form"
const uid __uid__
select "Envoyer un message a" "__to_name_list__" "=__to_name_default__" &to_name
Textbox "Votre Nom" #80 &from_name
email "Votre courriel" #80 &from_email
select "Club (Affiliation)" "Aucun/Autre|ACC_Montreal|ACC_Autre" ="Aucun/Autre" &club !
textbox "Numero de Membre (ACC)" ! #20 &club_nb !
Textbox "Sujet" #80 &subject
textarea "Message" x10 #5000 &msg
submit "Envoyer votre message" ',
            'mailSubjectFmt' => '__subject__',
            'mailBodyFmt' => '__msg__',
            'error_toNotAllowed' => 'Vous ne pouvez pas envoyer un message a __to_name__. SVP reviser votre selection.',
            'error_club_nb' => 'SVP forunir votre Numero de Membre ou selectionner Aucun/Autre'
        ),
        'en' => array(
            'mailSent'       => 'Your message was sent sucessfully',
            'mailSentError'  => 'There was an error sending your email, please contact the admin at ',
            'formDescFmt' => '
name accmtl_contact
action mail web@accmontreal.ca
Thanks "Your message was sent sucessfully. Thank you!"

Fieldset "ACC Montreal Contact Form"
const uid __uid__
select "Send Message To" "__to_name_list__" "=__to_name_default__" &to_name
Textbox "Your Name" #80 &from_name
email "Your e-mail" #80 &from_email
select "Club Affiliation" "None/Other|ACC_Montreal|ACC_Others" ="None/Other" &club !
textbox "Membership Number (ACC)" ! #20 &club_nb !
Textbox "Subject" #80 &subject
textarea "Message" x10 #5000 &msg
submit "Send your message" ',
            'mailSubjectFmt' => '__subject__',
            'mailBodyFmt' => '__msg__',
            'error_toNotAllowed' => 'Not allowed to send email to __to_name__. Please check your selection.',
            'error_club_nb' => 'Please provide your membership number or select None/Other'
        )
    );

    var $allowed_emails = array(
        'Information'       => 'info@accmontreal.ca',
        'President'         => 'president@accmontreal.ca',
        'National Rep.'     => 'nationalrep@accmontreal.ca',
        'Past Chair'        => 'pastchair@accmontreal.ca',
        'Vice President'    => 'vicepresident@accmontreal.ca',
        'Secretary'         => 'secretary@accmontreal.ca',
        'Treasurer'         => 'treasurer@accmontreal.ca',
        'Membership Rep.'   => 'membership@accmontreal.ca',
        'Marketing Rep.'    => 'marketing@accmontreal.ca',
        'Winter House Coor.'=> 'skihouse@accmontreal.ca',
        'Keene Farm Coor.'  => 'keenefarm@accmontreal.ca',
        'Socials Coor.'     => 'socials@accmontreal.ca',
        'Outings Coor.'     => 'outings@accmontreal.ca',
        'Leadership Coor.'  => 'leadership@accmontreal.ca',
        'Courses Coor.'     => 'courses@accmontreal.ca',
        'Environement Rep.' => 'environement2@accmontreal.ca',
        'Access  Rep.'      => 'environement2@accmontreal.ca',
        'Newsletter Editor' => 'newsletter@accmontreal.ca',
        'Webmaster'         => 'web@accmontreal.ca'
    );

    function _getFormDescFieldsVals($formDescFmtName) {
        //only one possible format, ignore $formDescFmtName
        $tonamelist = '';
        $tonamedef = false;
        foreach($this->allowed_emails as $k=>$v)  {
            if (!$tonamedef) $tonamedef = $k;
            if ($tonamelist) $tonamelist .= '|';
            $tonamelist .= $k;
        }
        return array(
            '__uid__' => uniqid(),
            '__to_name_list__' => $tonamelist,
            '__to_name_default__' => $tonamedef
        );
    }

    function _postcheckpost($data) {
        $errors = array();
        $values = $this->buro->_collectpost($data);
        $idx = 999;
        if (!isset($this->allowed_emails[$values['to_name']])) {
            $errors['to_name'] = 1;
            msg(str_replace('__to_name__',hsc($values['to_name']),$this->getLang('error_toNotAllowed')),-1);
        }
        // [club:number]
        if ($values['club']!='None/Other' && !$values['club_nb']) {
            $errors['club'] = 1;
            $errors['club_nb'] = 1;
            msg($this->getLang('error_club_nb'),-1);
        }
        return $errors;
    }

    function _action_ok($data,$thanks) {
        $values = $this->buro->_collectpost($data);

        $metaFile = $this->_metaFile_get();
        $metaFile = $this->_metaFile_add($metaFile,$values) ;
        $ok = $this->_metaFile_save($metaFile);

        $this->mailToAllowed = array_values($this->allowed_emails);
        if (isset($values['to_name']) && isset($this->allowed_emails[$values['to_name']]))
            $values[$this->mailToField] = $this->allowed_emails[$values['to_name']];
        $ok = $this->_sendMail($values);

        if ($ok) return $thanks;
        return false;
    }

}

$my_contact_form = new ACC_Form_Contact($this);
print($my_contact_form->_action_show_form());
